package io.renren.modules.offlinestudy.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import io.renren.common.utils.ExportExcel;
import io.renren.modules.offlinestudy.entity.SubscribeEntity;
import io.renren.modules.onlinestudy.entity.XcBmEntity;
import io.renren.modules.onlinestudy.entity.XyxxImage;
import io.renren.modules.onlinestudy.service.XcBmService;
import io.renren.modules.sys.entity.SysUserEntity;
import org.apache.commons.collections.map.HashedMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.offlinestudy.entity.SubscribedateEntity;
import io.renren.modules.offlinestudy.service.SubscribedateService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.common.utils.R;

import javax.servlet.http.HttpServletResponse;


/**
 * 
 * 
 * @author zhubsh
 * @email sd_zhusbh@163.com
 * @date 2018-10-31 09:49:39
 */
@RestController
@RequestMapping("offlinestudy/subscribedate")
public class SubscribedateController {
	@Autowired
	private SubscribedateService subscribedateService;

	@Autowired
	private XcBmService xcBmService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("offlinestudy:subscribedate:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<SubscribedateEntity> subscribedateList = subscribedateService.queryList(query);
		int total = subscribedateService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(subscribedateList, total, query.getLimit(), query.getCurrPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("offlinestudy:subscribedate:info")
	public R info(@PathVariable("id") Integer id){
		List<SubscribedateEntity> subscribedates = subscribedateService.queryObjects(id);
		return R.ok().put("list", subscribedates);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("offlinestudy:subscribedate:save")
	public R save(@RequestBody List<SubscribedateEntity> subscribedates){
		subscribedateService.save(subscribedates);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("offlinestudy:subscribedate:update")
	public R update(@RequestBody List<SubscribedateEntity> subscribedates){
		subscribedateService.update(subscribedates);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("offlinestudy:subscribedate:delete")
	public R delete(@RequestBody Integer[] ids){
		subscribedateService.deleteBatch(ids);
		
		return R.ok();
	}

/**
 *              ******************以下为线下预约功能*******************************
 */

    /**
     * 列表
     */
    @RequestMapping("/subscribeList")
    @RequiresPermissions("offlinestudy:subscribe:list")
    public R subscribeList(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);

        List<SubscribeEntity> subscribeList = subscribedateService.querySubscribeList(query);
        int total = subscribedateService.querySubscribeTotal(query);

        PageUtils pageUtil = new PageUtils(subscribeList, total, query.getLimit(), query.getCurrPage());

        return R.ok().put("page", pageUtil);
    }


    /**
     * 信息
     */
    @RequestMapping("/subscribeInfo/{id}")
    @RequiresPermissions("offlinestudy:subscribe:info")
    public R subscribeInfo(@PathVariable("id") Integer id){
        SubscribeEntity subscribe = subscribedateService.querySubcribeObject(id);

        return R.ok().put("subscribe", subscribe);
    }

    /**
     * 保存
     */
    @RequestMapping("/subscribeSave")
    @RequiresPermissions("offlinestudy:subscribe:save")
    public R subscribeSave(@RequestBody SubscribeEntity subscribe){
        SysUserEntity sysUserEntity = (SysUserEntity) SecurityUtils.getSubject().getPrincipal();
        subscribe.setUserid(String.valueOf(sysUserEntity.getUserId()));
        subscribedateService.subscribeSave(subscribe);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/subscribeUpdate")
    @RequiresPermissions("offlinestudy:subscribe:update")
    public R subscribeUpdate(@RequestBody SubscribeEntity subscribe){
        subscribedateService.subscribeUpdate(subscribe);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/subscribeDelete")
    @RequiresPermissions("offlinestudy:subscribe:delete")
    public R subscribeDelete(@RequestBody Integer[] ids){
        subscribedateService.subscribeDeleteBatch(ids);

        return R.ok();
    }


    /**
     * 查询当前可以预约的学习时间
     * @return
     */
    @RequestMapping("/selectValidateSubscribeDate")
    public R selectValidateSubscribeDate(@RequestParam String studytype){
        LocalDate localDate = LocalDate.now();
        List<SubscribedateEntity> subscribedateEntities = subscribedateService.selectValidateSubscribeDate(localDate.toString(),studytype);
        Map<String, Object> map = new HashedMap();
        map.put("list", subscribedateEntities);
        return R.ok(map);
    }

    @RequestMapping("/exceportExcel")
    public void exceportExcel(@RequestParam Map<String, Object> params,HttpServletResponse response){
        String td = "name,sfzh,tel,dabh,studytype_,jclx_,yuyuetime";
        String titletd = "姓名,身份证号,手机号码,档案编号,预约类型,准驾车型,学习周期";
        String[] param=td.split(",");
        String[] Title=titletd.split(",");
        //查询列表数据
        Query query = new Query(params);
        List<Map<String,Object>> subscribeList = subscribedateService.selSubscribeExcel(query);
        ExportExcel excel=new ExportExcel();
        excel.exportExcel("学员信息表.xls", Title,param, subscribeList, response);
    }

    @RequestMapping("/printStudy")
    public R printStudy(@RequestParam Integer id){
        Map<String, Object> result_map = new HashedMap();
        SubscribeEntity subscribeEntity = subscribedateService.querySubcribeObject(id);
        XcBmEntity xcBmEntity = subscribedateService.selXyxx(subscribeEntity.getUserid());
        if(xcBmEntity != null) {
            List<XyxxImage> xyxxImages = xcBmService.selImgArrs(xcBmEntity);
            result_map.put("imgList", xyxxImages);
        }
        result_map.put("xxxx",subscribeEntity);

        return R.ok(result_map);
    }
	
}
